package com.wisdom.benefit.jufa.news;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.text.TextUtils;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;

import com.wisdom.benefit.jufa.R;
import com.wisdom.benefit.jufa.base.BaseApplication;
import com.wisdom.benefit.jufa.base.BaseNewFragment;
import com.wisdom.benefit.jufa.main.CalendarViewActivity;
import com.wisdom.benefit.jufa.main.HomeFragment;
import com.wisdom.benefit.jufa.net.RetrofitService;
import com.wisdom.benefit.jufa.utils.CommonUtils;

import androidx.annotation.Nullable;
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;

public class OrderFragment extends BaseNewFragment implements View.OnClickListener, SwipeRefreshLayout.OnRefreshListener {
    public static final String TAG = HomeFragment.class.getSimpleName();
    private static final String[] name = {"全部", "支付宝", "微信", "支付宝转账"};
    private static final String[] type = {"全部", "未付款", "已付款", "申诉中", "已失效", "已完成"};
    private Spinner spinner, spinner1;
    private ArrayAdapter<String> adapter, typeAdapter;
    private Button btn;
    private TextView todayTv, yesTv, sevenTv, monthTv, startTv, endTv, orderIdTv, orderNoTv, actualMoneyTv;
    private RelativeLayout startTimeRl, endTimeRl;
    private int i = 0;
    private Long payeeId ;
    private String orderNo, startTime, endTime,orderId;
    private double actualMoney;
    private int payType, status;
    private ImageView startIv, endIv;
    SwipeRefreshLayout refreshLayout;


    public static OrderFragment newInstance() {
        Bundle args = new Bundle();
        OrderFragment fragment = new OrderFragment();
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        queryUserOrderPayIncomeData(Long.valueOf(BaseApplication.getContext().getUserId()).longValue());
    }

    @Override
    public int setLayoutId() {
        return R.layout.fragment_order;
    }

    @Override
    public void findViewById(View view) {
        //下拉刷新
        refreshLayout = view.findViewById(R.id.refreshLayout);
        //查看订单流水
        btn = view.findViewById(R.id.order_btn);
        spinner = view.findViewById(R.id.spinner);
        spinner1 = view.findViewById(R.id.spinner_2);
        //将可选内容与ArrayAdapter连接起来，simple_spinner_item是android系统自带样式
        adapter = new ArrayAdapter<String>(getActivity(), android.R.layout.simple_spinner_item, name);
        //设置下拉列表的风格,simple_spinner_dropdown_item是android系统自带的样式，等会自定义修改
        adapter.setDropDownViewResource(android.R.layout.simple_list_item_single_choice);
        //将adapter 添加到spinner中
        spinner.setAdapter(adapter);
        //将可选内容与ArrayAdapter连接起来，simple_spinner_item是android系统自带样式
        typeAdapter = new ArrayAdapter<String>(getActivity(), android.R.layout.simple_spinner_item, type);
        //设置下拉列表的风格,simple_spinner_dropdown_item是android系统自带的样式，等会自定义修改
        typeAdapter.setDropDownViewResource(android.R.layout.simple_list_item_single_choice);
        spinner1.setAdapter(typeAdapter);
        //今日收入
        todayTv = view.findViewById(R.id.order_today_tv);
        //昨日收入
        yesTv = view.findViewById(R.id.order_yes_tv);
        //七天收入
        sevenTv = view.findViewById(R.id.order_seven_tv);
        //一个月收入
        monthTv = view.findViewById(R.id.order_month_tv);
        //开始时间
        startTimeRl = view.findViewById(R.id.order_start_time_rl);
        startTv = view.findViewById(R.id.order_start_tv);
        //结束时间
        endTimeRl = view.findViewById(R.id.order_end_time_rl);
        endTv = view.findViewById(R.id.order_end_tv);
        //订单号
        orderIdTv = view.findViewById(R.id.order_orderId_tv);
        //商户号
        orderNoTv = view.findViewById(R.id.order_orderNo_tv);
        //实付金额
        actualMoneyTv = view.findViewById(R.id.order_actualMoney_tv);

        startIv = view.findViewById(R.id.order_start_del_iv);
        endIv = view.findViewById(R.id.order_end_del_iv);
    }

    @Override
    public void setViewData(View view) {
        //自定义刷新控件颜色
        refreshLayout.setColorSchemeResources(R.color.blue);
    }

    @Override
    public void setClickEvent(View view) {
        //刷新
        refreshLayout.setOnRefreshListener(this);
        //查看订单流水
        btn.setOnClickListener(this);
        //开始时间
        startTimeRl.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent districtIntent1 = new Intent(getActivity(), CalendarViewActivity.class);
                districtIntent1.putExtra("flag", 103);
                startActivity(districtIntent1);
                i = 1;
            }
        });
        //结束时间
        endTimeRl.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent districtIntent2 = new Intent(getActivity(), CalendarViewActivity.class);
                districtIntent2.putExtra("flag", 104);
                startActivity(districtIntent2);
                i = 2;
            }
        });
        startIv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                startTv.setText("请选择开始时间");
            }
        });
        endIv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                endTv.setText("请选择结束时间");
            }
        });
        spinner.setOnItemSelectedListener(new Spinner.OnItemSelectedListener() {//选择item的选择点击监听事件
            public void onItemSelected(AdapterView<?> arg0, View arg1,
                                       int arg2, long arg3) {
                if (arg2 == 1) {
                    payType = 1;
                } else if (arg2 == 2) {
                    payType = 2;
                } else if (arg2 == 3) {
                    payType = 3;
                } else if (arg2 == 0) {
                    payType = 0;
                }


            }

            public void onNothingSelected(AdapterView<?> arg0) {

            }
        });

        spinner1.setOnItemSelectedListener(new Spinner.OnItemSelectedListener() {//选择item的选择点击监听事件
            public void onItemSelected(AdapterView<?> arg0, View arg1,
                                       int arg2, long arg3) {
                if (arg2 == 1) {
                    status = 1;
                } else if (arg2 == 2) {
                    status = 2;
                } else if (arg2 == 3) {
                    status = 3;
                } else if (arg2 == 4) {
                    status = 4;
                } else if (arg2 == 5) {
                    status = 5;
                }else if (arg2 == 0) {
                    status = 0;
                }

            }

            public void onNothingSelected(AdapterView<?> arg0) {

            }
        });

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.order_btn:
                if (!TextUtils.isEmpty(orderIdTv.getText().toString())) {
                    orderId = orderIdTv.getText().toString();
                } else {
                    orderId = "";
                }
                if (!TextUtils.isEmpty(orderNoTv.getText().toString())) {
                    orderNo = orderNoTv.getText().toString();
                } else {
                    orderNo = "";
                }
                if (!TextUtils.isEmpty(actualMoneyTv.getText().toString())) {
                    actualMoney = Double.parseDouble(actualMoneyTv.getText().toString());
                } else {
                    actualMoney = 0.0;
                }
                if (!startTv.getText().toString().equals("请选择开始时间")) {
                    if (endTv.getText().toString().equals("请选择结束时间")) {
                        Toast.makeText(mActivity, "请选择结束时间", Toast.LENGTH_SHORT).show();
                        return;
                    } else {
                        startTime = startTv.getText().toString();
                    }
                } else {
                    startTime = "";
                }
                if (!endTv.getText().toString().equals("请选择结束时间")) {
                    if (startTv.getText().toString().equals("请选择开始时间")) {
                        Toast.makeText(mActivity, "请选择开始时间", Toast.LENGTH_SHORT).show();
                        return;
                    } else {
                        startTime = startTv.getText().toString();
                    }
                    endTime = endTv.getText().toString();
                } else {
                    endTime = "";
                }
                Intent intent = new Intent(getActivity(), OrderDetailsActivity.class);
                intent.putExtra("orderId", orderId);
                intent.putExtra("orderNo", orderNo);
                intent.putExtra("actualMoney", actualMoney);
                intent.putExtra("startTime", startTime);
                intent.putExtra("endTime", endTime);
                intent.putExtra("status", status);
                intent.putExtra("payType", payType);
                startActivity(intent);
                break;
        }
    }

    /**
     * 订单收入情况查询接口
     */
    private void queryUserOrderPayIncomeData(Long userIdCreate) {
        addDisposable(RetrofitService.getNetService()
                .queryUserOrderPayIncomeData(userIdCreate)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(response -> {
                    if (response.getCode() == 0) {
                        todayTv.setText(String.valueOf(response.getData().getTodayIncome()));
                        yesTv.setText(String.valueOf(response.getData().getYesterdayIncome()));
                        sevenTv.setText(String.valueOf(response.getData().getSevenDaysIncome()));
                        monthTv.setText(String.valueOf(response.getData().getThirtyDaysIncome()));
                    } else {
                        showBindToast(response.getMsg());
                    }
                }, throwable -> {
                    throwable.printStackTrace();
                    hideProgressDialog();
                    if (CommonUtils.isNetworkAvailable(getContext()))
                        showBindToast("查询失败");
                    else
                        showBindToast("无网络连接，请稍后再试");
                }));
    }


    @Override
    public void onResume() {
        super.onResume();
        queryUserOrderPayIncomeData(Long.valueOf(BaseApplication.getContext().getUserId()).longValue());
        if (BaseApplication.getContext().getIsInput().toString().equals("1")) {
            BaseApplication.getContext().setIsInput("0");
            if (i == 1) {
                i = 0;
                startTv.setText(BaseApplication.getContext().getTime().toString());
            } else if (i == 2) {
                i = 0;
                endTv.setText(BaseApplication.getContext().getTime().toString());
            }
        }
    }


    @Override
    public void onRefresh() {
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                refreshLayout.setVisibility(View.VISIBLE);
                refreshLayout.setRefreshing(false);
                queryUserOrderPayIncomeData(Long.valueOf(BaseApplication.getContext().getUserId()).longValue());
            }
        }, 1000);
    }
}
